package com.example.activity.controller;

import com.example.activity.pojo.GeaUser;
import com.example.activity.service.UserService;
import com.example.activity.service.WorkFlowService;
import lombok.extern.slf4j.Slf4j;
import org.activiti.engine.*;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.example.activity.pojo.User;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

@RestController
@RequestMapping("/wf")
@Slf4j
public class WorkFlowController {

    @Autowired
    /**
     * 管理流程定义
     * */
    private RepositoryService repositoryService;
    @Autowired
    /**
     * 执行管理、包括启动、推进、删除流程实例等操作
     * */
    private RuntimeService runtimeService;
    @Autowired
    /**
     * 任务管理
     * */
    private TaskService taskService;

    /**
     * 用户服务
     */
    @Autowired
    UserService userService;

    /**
     * 工作流服务
     */
    @Autowired
    WorkFlowService workFlowService;


/*
    @RequestMapping("user-home")
    @ResponseBody
    public ModelAndView startdeomo(ModelAndView modelAndView) {
        modelAndView.setViewName("user-home");
        if (taskService.createTaskQuery().list()!=null){
            modelAndView.addObject("taskList", taskService.createTaskQuery().list());
            return modelAndView;
        }

        return modelAndView;
    }*/

    /**
     * 显示可以进行的流程定义
     */
    @RequestMapping("start")
    public ModelAndView deployment(ModelAndView modelAndView) {
        modelAndView.setViewName("start");

        modelAndView.addObject("deploymentList", repositoryService.createProcessDefinitionQuery().list());
        return modelAndView;
    }


    /**
     * 第一个案例
     * @return
     */
    @RequestMapping("/firstDemo")
    public ModelAndView startdemo() {
        Map result = new HashMap();
        GeaUser user = new GeaUser();
        Scanner scanner = new Scanner(System.in);
        ProcessInstance holiday = runtimeService.startProcessInstanceByKey("holiday");
        Task task = taskService.createTaskQuery().processInstanceId(holiday.getId()).singleResult();

        if (holiday.getProcessDefinitionName().equals("holiday")) {
            System.out.println("请输入多少天");
           // user.setDay();
          //  user.setStatus(1);
            user.setName(task.getAssignee());
            userService.save(user);
            result.put("user", user);
            taskService.complete(taskService.createTaskQuery().processInstanceId(holiday.getId()).singleResult().getId(), result);

        }
        ModelAndView modelAndView = new ModelAndView("/demo/user-home");
        return modelAndView;

    }

    /**
     *
     * 任务的同意 拒绝  放弃
     *
     * */
    @RequestMapping("/consent")
    public ModelAndView consent(@RequestParam("taskId") String taskId, @RequestParam("status") String status) {
        User user = new User();
        Map result = new HashMap();

        if (taskId != null) {
            Task task = taskService.createTaskQuery().taskId(taskId).singleResult();
            user.setStatus(Integer.parseInt(status));
            result.put("user", user);
            if (task != null) {
                if (status.equals("0")) {
                    taskService.complete(taskId, result);
                    ModelAndView modelAndView = new ModelAndView(new RedirectView("/demo/user-home"));
                    return modelAndView;
                } else if (status.equals("3")) {
                    taskService.complete(taskId, result);
                } else {
                    taskService.complete(taskId, result);
                }
            }
        }
        ModelAndView modelAndView = new ModelAndView(new RedirectView("/demo/user-home"));
        return modelAndView;

    }



    /**
     * 返回历史图像,显示所有已完成的任务。
     *
     * @param processId
     * @param response
     * @return
     */
    @RequestMapping("/viewProcessImg/{processId}")
    public boolean viewProcessImg(@PathVariable("processId") String processId, HttpServletResponse response) {
        try {
            byte[] processImage = workFlowService.getProcessImage(processId);
            System.out.println(processImage.length);
            OutputStream outputStream = response.getOutputStream();
            response.setContentType("image/png");
            outputStream.write(processImage);
            outputStream.flush();
            outputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
            log.error("viewProcessImg---- {}", e.getLocalizedMessage());
            return false;
        }

        return true;
    }

}
